package cn.iocoder.yudao.module.monitor.controller.admin.realtime;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.monitor.controller.admin.realtime.vo.*;
import cn.iocoder.yudao.module.monitor.dal.dataobject.realtime.RealTimeDO;
import cn.iocoder.yudao.module.monitor.convert.realtime.RealTimeConvert;
import cn.iocoder.yudao.module.monitor.service.realtime.RealTimeService;

@Api(tags = "管理后台 - 监控")
@RestController
@RequestMapping("/monitor/real-time")
@Validated
public class RealTimeController {

    @Resource
    private RealTimeService realTimeService;

    @PostMapping("/create")
    @ApiOperation("创建监控")
    @PreAuthorize("@ss.hasPermission('monitor:real-time:create')")
    public CommonResult<Long> createRealTime(@Valid @RequestBody RealTimeCreateReqVO createReqVO) {
        return success(realTimeService.createRealTime(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新监控")
    @PreAuthorize("@ss.hasPermission('monitor:real-time:update')")
    public CommonResult<Boolean> updateRealTime(@Valid @RequestBody RealTimeUpdateReqVO updateReqVO) {
        realTimeService.updateRealTime(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除监控")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('monitor:real-time:delete')")
    public CommonResult<Boolean> deleteRealTime(@RequestParam("id") Long id) {
        realTimeService.deleteRealTime(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得监控")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('monitor:real-time:query')")
    public CommonResult<RealTimeRespVO> getRealTime(@RequestParam("id") Long id) {
        RealTimeDO realTime = realTimeService.getRealTime(id);
        return success(RealTimeConvert.INSTANCE.convert(realTime));
    }

    @GetMapping("/list")
    @ApiOperation("获得监控列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('monitor:real-time:query')")
    public CommonResult<List<RealTimeRespVO>> getRealTimeList(@RequestParam("ids") Collection<Long> ids) {
        List<RealTimeDO> list = realTimeService.getRealTimeList(ids);
        return success(RealTimeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得监控分页")
    @PreAuthorize("@ss.hasPermission('monitor:real-time:query')")
    public CommonResult<PageResult<RealTimeRespVO>> getRealTimePage(@Valid RealTimePageReqVO pageVO) {
        PageResult<RealTimeDO> pageResult = realTimeService.getRealTimePage(pageVO);
        return success(RealTimeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出监控 Excel")
    @PreAuthorize("@ss.hasPermission('monitor:real-time:export')")
    @OperateLog(type = EXPORT)
    public void exportRealTimeExcel(@Valid RealTimeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<RealTimeDO> list = realTimeService.getRealTimeList(exportReqVO);
        // 导出 Excel
        List<RealTimeExcelVO> datas = RealTimeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "监控.xls", "数据", RealTimeExcelVO.class, datas);
    }

}
